<template>
  <div>
    <div class="top">
      <van-nav-bar
        title="收货地址"
        left-text=""
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <div class="main">
      <!-- 	onAdd点击新增按钮时触发edit点击编辑按钮时触发select item: 地址对象，index: 索引-->
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        show-set-default
      show-search-result
        disabled-text="以下地址超出配送范围"
        @add="onAdd"
        @edit="onEdit"
        @select="onselsect"
        aria-checked:t
          default-tag-text="默认"
          switchable:false
      />
    </div>
    <!-- @select="onselsect" -->
  </div>
</template>

<script>
import { getAddress, AddressDelete } from "../../request/api";
import eventBus from "@/eventBus"
export default {
  data() {
    return {
      chosenAddressId: "1",
      list: [],
      deletelist: "",
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },

    onAdd() {
      this.$router.push("/order/newAddress");
    },
    onEdit(item, index) {
      // console.log(item.addressid);
      eventBus.$emit("xiaoming",item)
      AddressDelete({
        addressid: item.addressid,
        token: localStorage.getItem("token"),
      }).then((res) => {
        console.log(res.data);
        // 页面刷新
        location.reload();
      });

      // this.$router.push("/order/editaddress" );
    },
    onselsect(item, index) {
      //  for(let i=0;i<list.length;i++){
      item.isDefault = false;
    },
  },
  created() {
    getAddress({
      token: localStorage.getItem("token"),
      userid: localStorage.getItem("userid"),
    }).then((res) => {
      // console.log(res.data.data);
      this.list = res.data.data;
      this.list.id = res.data.data.index;
      for (let i = 0; i < this.list.length; i++) {
        this.list[i].address =
          res.data.data[i].city + res.data.data[i].addressDetail;
        this.list[i].isDefault = res.data.data[i].isDefault;
      }
    });
  },
};
</script>

<style  scoped>
</style>>
